package algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 *  字符串的无重复最长子串
 */
public class LongestNonRepeatingSubstring {
    private void solve(String str) {

        int left = 0;
        int right = 0;
        int max = 0;
        List<Character> list = new ArrayList<>();
        while (right < str.length()) {
            while (right < str.length() && !list.contains(str.charAt(right))) {
                list.add(str.charAt(right));
                ++right;
            }
//            System.out.println(str.substring(left, right) + " length : " + (right-left));
            max = max > (right-left) ? max : (right - left);

            while (left != right && right < str.length() && list.contains(str.charAt(right)) ) {
                list.remove(0);
                left++;
            }
        }

        System.out.println(max);
    }

    public int lengthOfLongestSubstring(String s) {
        int maxLength = 0;
        List<Character> list = new ArrayList<>();
        for(int i=0; i< s.length(); i++) {
            if(!list.contains(s.charAt(i))) {
                list.add(s.charAt(i));
            } else {
                maxLength = Math.max(maxLength, list.size());
                while(!list.isEmpty() && list.contains(s.charAt(i))) {
                    list.remove(0);
                }
                list.add(s.charAt(i));
            }
        }
        return Math.max(maxLength, list.size());
    }

    public static void main(String []args) {
        new LongestNonRepeatingSubstring().solve("pwwkew");
    }
}
